#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort/QSerialPortInfo>
#include <QtSerialPort/QSerialPort>
#include <QDebug>
#include <QObject>
#include <windows.h>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_serial(new QSerialPort(this))
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/favicon.ico"));
    m_console = new Console(ui->widget);
    m_console->setEnabled(true);
//    m_console->setLocalEchoEnabled(true);
    connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
    connect(m_console, &Console::getData, this, &MainWindow::writeData);

    ui->cb_CMD_NO->clear();
    //通过QSerialPortInfo查找可用串口
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        ui->cb_CMD_NO->addItem(info.portName());
    }

    ui->cb_CMD_BR->addItem("115200");
    ui->cb_CMD_BR->addItem("9600");

//    QObject::connect(&serial_CMD.port, &QSerialPort::readyRead, this, &MainWindow::serial_CMD_read);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pb_CMD_clicked()
{
     qDebug("on_pb_CMD_clicked");
     if(ui->pb_CMD->text() == "打开")
     {
         m_serial->setPortName(ui->cb_CMD_NO->currentText());
         m_serial->setBaudRate(ui->cb_CMD_BR->currentText().toInt());
         m_serial->setDataBits(QSerialPort::Data8);
         m_serial->setParity(QSerialPort::NoParity);
         m_serial->setStopBits(QSerialPort::OneStop);
         m_serial->setFlowControl(QSerialPort::NoFlowControl);
         bool ret = m_serial->open(QIODevice::ReadWrite);

/*
        qDebug() << ui->cb_CMD_NO->currentText() << endl;
        serial_CMD.port.setPortName(ui->cb_CMD_NO->currentText());
        serial_CMD.port.setBaudRate(ui->cb_CMD_BR->currentText().toInt());
        serial_CMD.port.setDataBits(QSerialPort::Data8);
        serial_CMD.port.setParity(QSerialPort::NoParity);
        serial_CMD.port.setStopBits(QSerialPort::OneStop);
        serial_CMD.port.setFlowControl(QSerialPort::NoFlowControl);
        bool ret = serial_CMD.port.open(QIODevice::ReadWrite);
*/
        if(ret == false)
        {
            qDebug() << "CMD Open failed " << ui->cb_CMD_NO->currentText() << endl;
            QMessageBox::information(NULL, "提示", "串口不存在或被占用           ", QMessageBox::Yes, QMessageBox::Yes);
            return;
        }
        ui->pb_CMD->setText("关闭");
     }
     else
     {
         m_serial->close();
         ui->pb_CMD->setText("打开");
     }
}

void MainWindow::serial_CMD_read()
{
    QByteArray a = serial_CMD.port.readAll();
    QString s(a);
    qDebug() << s <<endl;
}

void MainWindow::writeData(const QByteArray &data)
{
    m_serial->write(data);
    qDebug()<<data<<endl;
}
//! [6]

//! [7]
void MainWindow::readData()
{
    const QByteArray data = m_serial->readAll();
    m_console->putData(data);
}

void MainWindow::on_pushButton_clicked()
{
//    m_serial->write((ui->lineEdit->text() + "\n").toUtf8());
//    m_console->putData((ui->lineEdit->text() + "").toUtf8());
}

